//
//  DetailFrameViewController.m
//  cam
//
//  Created by VIETHUNG on 1/6/14.
//  Copyright (c) 2014 DanielLe. All rights reserved.
//

#import "DetailFrameViewController.h"
#import "KGModal.h"
#import "DataSingleton.h"
#import <QuartzCore/QuartzCore.h>
#import "ELCImagePickerController.h"
#import "ELCAlbumPickerController.h"
#import "ELCAssetTablePicker.h"
#import "ViewFrame.h"
#import "KxMenu.h"

@interface DetailFrameViewController ()

@end

@implementation DetailFrameViewController
@synthesize arrayImage, imgFace;
@synthesize arrFace;
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initMenu];
    arrFace = [NSMutableArray array];
    [self initScrollView];
	
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initMenu
{
    UIView *twitterItem = [[UIView alloc] initWithFrame:CGRectMake(15, 15, 40, 40)];
    [twitterItem setMenuActionWithBlock:^{
        NSLog(@"tapped twitter item");
        [self.sideMenu close];
        [self shareTW];
    }];
    UIImageView *twitterIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [twitterIcon setImage:[UIImage imageNamed:@"tw.png"]];
    [twitterItem addSubview:twitterIcon];
    
    UIView *emailItem = [[UIView alloc] initWithFrame:CGRectMake(15, 15, 40, 40)];
    [emailItem setMenuActionWithBlock:^{
        NSLog(@"tapped email item");
        [self.sideMenu close];
    }];
    UIImageView *emailIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30 , 30)];
    [emailIcon setImage:[UIImage imageNamed:@"h2.jpg"]];
    [emailItem addSubview:emailIcon];
    
    UIView *facebookItem = [[UIView alloc] initWithFrame:CGRectMake(15, 15, 40, 40)];
    [facebookItem setMenuActionWithBlock:^{
        [self.sideMenu close];
        [self shareFB];
        
    }];
    UIImageView *facebookIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [facebookIcon setImage:[UIImage imageNamed:@"fb.png"]];
    [facebookItem addSubview:facebookIcon];
    
    UIView *browserItem = [[UIView alloc] initWithFrame:CGRectMake(15, 15, 40, 40)];
    [browserItem setMenuActionWithBlock:^{
        [self.sideMenu close];
        NSLog(@"tapped browser item");
    }];
    UIImageView *browserIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [browserIcon setImage:[UIImage imageNamed:@"save.png"]];
    [browserItem addSubview:browserIcon];
    
    self.sideMenu = [[HMSideMenu alloc] initWithItems:@[twitterItem, emailItem, facebookItem, browserItem]];
    [self.sideMenu setItemSpacing:5.0f];
    self.sideMenu.menuPosition = HMSideMenuPositionRight;
    [mViewFrame addSubview:self.sideMenu];
}

- (void)initScrollView
{
    DataSingleton *data = [DataSingleton shareManager];
    arrayImage = [data arrayItem:0];
    int size = mScrollView.frame.size.height;
    int space = 5;
    for (int i = 0; i < arrayImage.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 6.0;
        [btn setFrame:CGRectMake(i * (space + size), 0, size, size)];
        [btn setImage:[UIImage imageNamed:[arrayImage objectAtIndex:i]] forState:UIControlStateNormal];
        btn.tag = i;
        [btn addTarget:self action:@selector(pickerFrame:) forControlEvents:UIControlEventTouchUpInside];
        [mScrollView addSubview:btn];
    }
    [mScrollView setContentSize:CGSizeMake((space + size) * arrayImage.count, size)];
}

- (void)pickerFrame:(UIButton*)sender
{
    NSLog(@"%d",sender.tag);
    imgFrame.image = [UIImage imageNamed:[arrayImage objectAtIndex:sender.tag]];
}

- (IBAction)act_back:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)act_share:(id)sender {
    if (self.sideMenu.isOpen)
        [self.sideMenu close];
    else
        [self.sideMenu open];
}

- (IBAction)act_select:(UIButton*)sender {
    
    NSArray *menuItems =
    @[
      [KxMenuItem menuItem:@"Library" image:[UIImage imageNamed:@""] target:self action:@selector(act_library)],
      [KxMenuItem menuItem:@"Camera" image:[UIImage imageNamed:@""] target:self action:@selector(act_camera)],
      ];
    
    [KxMenu showMenuInView:self.view
                  fromRect:sender.frame
                 menuItems:menuItems];
}

- (IBAction)act_scroll:(UIButton*)sender {
    NSArray *menuItems =
    @[
      [KxMenuItem menuItem:@"Background" image:[UIImage imageNamed:@""] target:self action:@selector(act_background:)],
      [KxMenuItem menuItem:@"Frame" image:[UIImage imageNamed:@""] target:self action:@selector(act_frame:)],
      ];
    [KxMenu showMenuInView:self.view
                  fromRect:sender.frame
                 menuItems:menuItems];
}

- (void)act_camera
{
    [[KGModal sharedInstance] hide];
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
	picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
	[self presentModalViewController:picker animated:YES];
}

-(void)act_library
{
    [[KGModal sharedInstance] hide];
    ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initImagePicker];
    elcPicker.maximumImagesCount = 10;
    elcPicker.returnsOriginalImage = NO; //Only return the fullScreenImage, not the fullResolutionImage
	elcPicker.imagePickerDelegate = self;
    
    [self presentViewController:elcPicker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
	[picker dismissModalViewControllerAnimated:YES];
//	imgFace.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
{
    [picker dismissModalViewControllerAnimated:YES];
}

#pragma mark - Kxmenu
- (void)act_background:(id)sender
{
    NSLog(@"%@", sender);
}

- (void)act_frame:(id)sender
{
    NSLog(@"%@", sender);
}

#pragma mark - Share SNS
- (void)shareTW
{
    
}

- (void)shareFB
{
    
}

#pragma mark ELCImagePickerControllerDelegate Methods

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];
	
	for (int i = 0; i < info.count; i++) {
        NSDictionary *dict = [info objectAtIndex:i];
        UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];

        ViewFrame *view = [[ViewFrame alloc] initWithImage:image];
        CGFloat ratio = MIN( (0.5 * mViewFrame.width) / view.width, (0.5 * mViewFrame.height) / view.height);
        view.width  = ratio * view.width + view.imageView.frame.origin.x;
        view.height = ratio * view.height + view.imageView.frame.origin.y;
        view.center = CGPointMake(mViewFrame.width/2, mViewFrame.height/2);
        
        [mViewFrame addSubview:view];
        [ViewFrame setActiveStickerView:view];
        view.alpha = 0.2;
        [UIView animateWithDuration:0.3
                         animations:^{
                             view.alpha = 1;
                         }
         ];

	}
}

- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
